from bs4 import BeautifulSoup
import requests
import os
import re

headers = {
    'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'
}
booksList = []
try:
    page = int(input("请输入想要爬取的页数："))
except Exception as e:
    print('输入字符非法，请重新运行程序')
    os._exit(0)

for i in range(page):

    url = 'http://book.zongheng.com/store/c0/c0/b0/u0/p' + str(i+1) + '/v9/s9/t0/u0/i1/ALL.html'
    r = requests.get(url, headers=headers)

    soup = BeautifulSoup(r.content, 'html.parser')
    # print(soup.prettify)

    pattern = re.compile('bookbox f(.)')
    # books = soup.find_all('div', {'class': 'bookbox fl'})
    books = soup.find_all('div', {'class': pattern})


    for book in books:
        bookDic = {}
        bookDic['img'] = book.find('div', {'class': 'bookimg'}).find('img')['src']
        bookDic['name'] = book.find('div', {'class': 'bookname'}).text
        bookDic['intro'] = book.find('div', {'class': 'bookintro'}).text

        booksList.append(bookDic)

print(booksList[0])
print(len(booksList))
